package com.myway.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.myway.dao.FlyOrderMapper;
import com.myway.dao.FlyPassengeMapper;
import com.myway.dto.common.JsonData;
import com.myway.dto.util.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myway.dao.FlyTicketsMapper;
import com.myway.pojo.FlyOrder;
import com.myway.pojo.FlyPassenge;
import com.myway.pojo.FlyTickets;
import com.myway.service.FlyService;

@Service
public class FlyServiceImpl implements FlyService{

    @Autowired
    private FlyOrderMapper flyOrderMapper;

    @Autowired
    private FlyTicketsMapper flyTicketsMapper;

    @Autowired
    private FlyPassengeMapper flyPassengeMapper;

    @Override
    public JsonData addFlyOrder(FlyOrder flyOrder, List<FlyPassenge> flyPassenge, FlyTickets flyTickets) {
        String orderId= OrderNumberGenerator.getOrderNo()+"";

//        flyOrder.setId(OrderNumberGenerator.getOrderNo()+"");//自动生成
        flyOrder.setOrderId(orderId);
        flyOrder.setOrderStatus("未付款");
        flyOrder.setSetDate(new Date());
        long time=30*60*1000;
        flyOrder.setTimeLimit(new Date(new Date().getTime() + time));
        flyOrder.setAirlineCode("MJ");
        flyOrderMapper.insertSelective(flyOrder);


        Iterator<FlyPassenge> iterator = flyPassenge.iterator();
        while (iterator.hasNext()){
            FlyPassenge next = iterator.next();
//            next.setId(OrderNumberGenerator.getOrderNo()+"");//自动生成
            next.setNumber(orderId);
            next.setSetDate(new Date());
            flyPassengeMapper.insertSelective(next);
        }

        flyTickets.setId(OrderNumberGenerator.getOrderNo()+"");
        flyTickets.setOrderId(orderId);
        flyTickets.setSetDate(new Date());
        flyTicketsMapper.insertSelective(flyTickets);
        return JsonData.success();
    }
}
